package com.example.Objetos;

import java.text.DecimalFormat;
import java.util.List;

import com.example.transvipm.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Adapter extends BaseExpandableListAdapter {
	private List<Object> objects;
	private Activity activity;
	private LayoutInflater inflater;
	DecimalFormat formateador;

	public Adapter(Activity activity, List<Object> objects) {
		this.objects = objects;
		this.activity = activity;
		this.inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		formateador = new DecimalFormat("###,###.##");

	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return objects.get(groupPosition).getObjects().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		Object object = (Object) getChild(groupPosition, childPosition);

		CustomExpandableListView subObjects = (CustomExpandableListView) convertView;

		if (convertView == null) {

			subObjects = new CustomExpandableListView(activity);

		}
		Adapter2 adapter = new Adapter2(activity, object);
		subObjects.setAdapter(adapter);
		int[] colors = { 0xFF093C4D, 0xFF093C4D, 0xFF093C4D };

		subObjects.setChildDivider(new GradientDrawable(Orientation.RIGHT_LEFT,
				colors));
		
		subObjects.setGroupIndicator(activity.getResources().getDrawable(R.drawable.group_indicator));

		return subObjects;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return objects.get(groupPosition).getObjects().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return objects.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return objects.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		Object object = (Object) getGroup(groupPosition);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listview_element, null);
		}

		TextView name = (TextView) convertView.findViewById(R.id.name);
		TextView pax = (TextView) convertView.findViewById(R.id.pax);
		TextView pax_valor = (TextView) convertView
				.findViewById(R.id.pax_valor);
		TextView monto = (TextView) convertView.findViewById(R.id.monto);
		TextView monto_valor = (TextView) convertView
				.findViewById(R.id.monto_valor);

		name.setText(object.getName());
		pax.setText("Pasajeros: ");
		pax_valor.setText(formateador.format(object.getPasajeros()));
		monto.setText("Monto: ");
		monto_valor.setText("$"+formateador.format(object.getMonto()));

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}

class Adapter2 extends BaseExpandableListAdapter {
	private Object object;
	private LayoutInflater inflater;
	private Activity activity;
	DecimalFormat formateador;

	public Adapter2(Activity activity, Object object) {
		this.activity = activity;
		this.object = object;
		this.inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		formateador = new DecimalFormat("###,###.##");
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return object.getObjects().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		Object object = (Object) getChild(0, childPosition);

		if (object.getNro() == 0) {

		}
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listview_element_2, null);

			Resources r = activity.getResources();
			float px40 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
					40, r.getDisplayMetrics());
			convertView.setPadding(convertView.getPaddingLeft() + (int) px40,
					convertView.getPaddingTop(), convertView.getPaddingRight(),
					convertView.getPaddingBottom());
		}

		TextView num_pro = (TextView) convertView.findViewById(R.id.num_pro);
		TextView hora_ini = (TextView) convertView.findViewById(R.id.hor_ini);
		TextView hora_sal = (TextView) convertView.findViewById(R.id.hor_sal);
		TextView salida = (TextView) convertView.findViewById(R.id.Salida);
		TextView tipo_serv = (TextView) convertView.findViewById(R.id.tipo_ser);
		TextView zr = (TextView) convertView.findViewById(R.id.ZR);
		TextView pax = (TextView) convertView.findViewById(R.id.num_pax);
		TextView mont = (TextView) convertView.findViewById(R.id.num_monto);

		if (childPosition != 0) {

			if (childPosition % 2 == 1) {

				num_pro.setText(String.valueOf(object.getNro()));
				hora_ini.setText(object.getHoraIni());
				hora_sal.setText(object.getHoraFin());
				salida.setText(String.valueOf(object.getSalida()));
				tipo_serv.setText(object.getServicio().trim());
				zr.setText(object.getModo());
				pax.setText(String.valueOf(object.getPasajeros()));
				mont.setText("$"+formateador.format(object.getMonto()));

				num_pro.setBackgroundColor(0xFF2B5E6F);
				hora_ini.setBackgroundColor(0xFF2B5E6F);
				hora_sal.setBackgroundColor(0xFF2B5E6F);
				salida.setBackgroundColor(0xFF2B5E6F);
				tipo_serv.setBackgroundColor(0xFF2B5E6F);
				zr.setBackgroundColor(0xFF2B5E6F);
				pax.setBackgroundColor(0xFF2B5E6F);
				mont.setBackgroundColor(0xFF2B5E6F);

			} else {

				num_pro.setText(String.valueOf(object.getNro()));
				hora_ini.setText(object.getHoraIni());
				hora_sal.setText(object.getHoraFin());
				salida.setText(String.valueOf(object.getSalida()));
				tipo_serv.setText(object.getServicio().trim());
				zr.setText(object.getModo());
				pax.setText(String.valueOf(object.getPasajeros()));
				mont.setText("$"+formateador.format(object.getMonto()));
				
				num_pro.setBackgroundColor(0xFF093C4D);
				hora_ini.setBackgroundColor(0xFF093C4D);
				hora_sal.setBackgroundColor(0xFF093C4D);
				salida.setBackgroundColor(0xFF093C4D);
				tipo_serv.setBackgroundColor(0xFF093C4D);
				zr.setBackgroundColor(0xFF093C4D);
				pax.setBackgroundColor(0xFF093C4D);
				mont.setBackgroundColor(0xFF093C4D);

			}

		} else {

			num_pro.setText(String.valueOf(object.getNo()));
			hora_ini.setText(object.getHrini());
			hora_sal.setText(object.getHrfin());
			salida.setText(object.getSalida_text());
			tipo_serv.setText(object.getServicio_text());
			zr.setText(object.getModo_text());
			pax.setText(object.getPasajeros_text());
			mont.setText(object.getMonto_text());

			num_pro.setBackgroundColor(0xFFFE7D27);
			hora_ini.setBackgroundColor(0xFFFE7D27);
			hora_sal.setBackgroundColor(0xFFFE7D27);
			salida.setBackgroundColor(0xFFFE7D27);
			tipo_serv.setBackgroundColor(0xFFFE7D27);
			zr.setBackgroundColor(0xFFFE7D27);
			pax.setBackgroundColor(0xFFFE7D27);
			mont.setBackgroundColor(0xFFFE7D27);
		}

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return object.getObjects().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return object;
	}

	@Override
	public int getGroupCount() {
		return 1;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listview_element, null);
			Resources r = activity.getResources();
			float px20 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
					20, r.getDisplayMetrics());
			convertView.setPadding(convertView.getPaddingLeft() + (int) px20,
					convertView.getPaddingTop(), convertView.getPaddingRight(),
					convertView.getPaddingBottom());
		}

		TextView name = (TextView) convertView.findViewById(R.id.name);
		TextView pax = (TextView) convertView.findViewById(R.id.pax);
		TextView pax_valor = (TextView) convertView
				.findViewById(R.id.pax_valor);
		TextView monto = (TextView) convertView.findViewById(R.id.monto);
		TextView monto_valor = (TextView) convertView
				.findViewById(R.id.monto_valor);

		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) name
				.getLayoutParams();
		params.setMargins(0, 0, 150, 0);
		name.setLayoutParams(params);

		name.setText(object.getName());
		pax.setText("Pasajeros: ");
		pax_valor.setText(formateador.format(object.getPasajeros()));
		monto.setText("Monto: ");
		monto_valor.setText("$"+formateador.format(object.getMonto()));

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}